﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using SafoverWpfFw;
using System;
using Safover.LidarCollector.Core;
using Safover.LidarCollector.Managers;

namespace Safover.LidarCollector.ViewModel
{
    public class DeviceReadingsViewModel : BaseViewModel
    {
        /// <summary>
        /// 经度
        /// </summary>
        public double Longitude { get; set; }

        /// <summary>
        /// 纬度
        /// </summary>
        public double Latitude { get; set; }



        /// <summary>
        /// 内触发频率
        /// </summary>
        public double? InternalTriggerFrequency { get; set; }

        /// <summary>
        /// 电流实际值
        /// </summary>
        public double? ElectricityRealValue { get; set; }

        /// <summary>
        /// LD温度
        /// </summary>
        public double? LDTemperature { get; set; }

        /// <summary>
        /// 激光晶体温度
        /// </summary>
        public double? LaserCrystalTemperature { get; set; }


        /// <summary>
        /// 激光脉冲能量
        /// </summary>
        public double? LaserPulseEnergy { get; set; }

        /// <summary>
        /// 倍晶体温度
        /// </summary>
        public double? FrequencyDoublingCrystalTemperature { get; set; }

        /// <summary>
        /// 激光器是否发光中
        /// </summary>
        public bool IsLasarOpen { get; set; }

        /// <summary>
        /// 激光器工作时长
        /// </summary>
        public TimeSpan? LaserWorkingTime { get; protected set; } = TimeSpan.FromMinutes(1000.5);


        /// <summary>
        /// 俯仰角
        /// </summary>
        public float PitchAngle { get; set; }

        /// <summary>
        /// 方位角
        /// </summary>
        public float HeadingAngle { get; set; }

        /// <summary>
        /// 横滚角
        /// </summary>
        public float RollAngle { get; set; }


        public float? VerticalAngle { get; set; }

        public float? HorizontalAngle { get; set; }

        public bool IsRotaryTableMoving { get; set; }

        public DeviceReadingsViewModel() 
        {
            DeviceManager.DeviceFactoryInstance.DeviceReadingsUpdated += OnDeviceReadingsUpdated;
        }

        private void OnDeviceReadingsUpdated(object sender, DeviceReadingsUpdatedEventArgs e)
        {
            switch (e.DeviceType)
            {
                case DeviceType.OpticalMaser:
                    UpdateOpticalMaser(e as OpticalMaserReadingsUpdatedEventArgs);
                    break;
                case DeviceType.GpsLocator:
                    UpdateGps(e as GpsLocatorReadingsUpdatedEventArgs);
                    break;
                case DeviceType.ElectronicCompass:
                    UpdateElectronicCompass(e as ElectronicCompassReadingsUpdatedEventArgs);
                    break;
                case DeviceType.RotaryTable:
                    UpdateRotaryTable(e as RotaryTableReadingsUpdatedEventArgs);
                    break;
                case DeviceType.AcquisitionCard:
                    break;
                case DeviceType.Camera:
                    break;
                default:
                    break;
            }
        }

        private void UpdateRotaryTable(RotaryTableReadingsUpdatedEventArgs e)
        {
            VerticalAngle = e.VerticalAngle;
            HorizontalAngle = e.HorizontalAngle;
            IsRotaryTableMoving = e.IsRotaryTableMoving;
        }

        private void UpdateElectronicCompass(ElectronicCompassReadingsUpdatedEventArgs e)
        {
            PitchAngle = e.PitchAngle;
            RollAngle = e.RollAngle;
            HeadingAngle = e.HeadingAngle;
        }

        private void UpdateGps(GpsLocatorReadingsUpdatedEventArgs e)
        {
            Longitude = e.Longitude;
            Latitude = e.Latitude;
        }

        private void UpdateOpticalMaser(OpticalMaserReadingsUpdatedEventArgs e)
        {
            InternalTriggerFrequency = e.InternalTriggerFrequency;
            ElectricityRealValue = e.ElectricityRealValue;

            if (e.LDTemperature != null)
            {
                LDTemperature = Math.Round((double)e.LDTemperature, 2);
            }

            if (e.LaserCrystalTemperature != null)
            {
                LaserCrystalTemperature = Math.Round((double)e.LaserCrystalTemperature, 2);
            }

            if (e.FrequencyDoublingCrystalTemperature != null)
            {
                FrequencyDoublingCrystalTemperature = Math.Round((double)e.FrequencyDoublingCrystalTemperature, 2);
            }

            LaserPulseEnergy = FakeLaserPulseEnergy();

            LaserWorkingTime = e.LaserWorkingTime;
            IsLasarOpen = e.IsLasarOpen;
        }

        private int FakeLaserPulseEnergy()
        {
            var fakeSeed = DateTime.Now.Millisecond;
            var probability = fakeSeed % 10;

            int mod;
            if (probability == 0)
            {
                mod = 10;
            }
            else if (probability <= 1)
            {
                mod = 9;
            }
            else if (probability <= 3)
            {
                mod = 7;
            }
            else if (probability <= 6)
            {
                mod = 5;
            }
            else
            {
                mod = 3;
            }

            var sign = fakeSeed % 2 == 0 ? -1 : 1;
            var fakeOffset = 400 + DateTime.Now.Millisecond % mod * sign;

            return fakeOffset;
        }
    }
}
